package day11.file;

import java.io.File;
import java.util.HashMap;

/**
 * @author Xiao
 * @date 2021/7/31
 */
public class FileCase03 {
    /**
     * 4.统计一个文件夹中每种文件的个数并打印
     */
    public static void main(String[] args) {
        File dir = new File("E:\\develop\\Test");
        countFiles(dir);
        hm.forEach((suff, count) -> System.out.println(suff + ":" + count));
    }
    public static HashMap<String, Integer> hm = new HashMap<>();

    private static void countFiles(File dir) {
        File[] files = dir.listFiles();
        if (files != null) {
            for (File file : files) {
                if (file.isFile()) {
                    String s = file.getName();
                    String[] str = s.split("\\.");
                    String suff = str[str.length - 1];
                    if (hm.containsKey(suff)) {
                        int count = hm.get(suff);
                        hm.put(suff, count + 1);
                    } else {
                        hm.put(suff, 1);
                    }
                } else {
                    countFiles(file);
                }
            }
        }
    }
}
